import { Dimensions } from "@sapi-game/constants";
import { Region } from "@sapi-game/main";
import { CameraPreset, DimensionIds } from "@sapi-game/utils/vanila-data";
import { PKBuildState } from "@/parkour/buildState/buildState";
import { parkourGameModule } from "../module";
import { PKMapLoader } from "./loader";

export class PKLoadState extends parkourGameModule.State {
    /**游戏的最大区域 */
    readonly gameRegion = new Region.CubeRegion(
        DimensionIds.Overworld,
        { x: 788, y: -60, z: 575 },
        { x: 719, y: -24, z: 608 }
    );

    override onEnter(): void {
        this.initPlayer();
        this.addComponent(PKMapLoader);
    }

    toNext() {
        this.transitionTo(PKBuildState);
    }

    initPlayer() {
        this.context.groupSet.runCommand("clear @s");
        this.context.groupSet.runCommand("gamemode 2");
        //传送玩家，显示标题，设置摄像头
        this.context.groupSet.forEach((p) => {
            p.player.teleport(this.context.spawn, {
                dimension: Dimensions.Overworld,
            });
            p.player.onScreenDisplay.setTitle("地图重置中...", {
                stayDuration: 100,
                fadeInDuration: 20,
                fadeOutDuration: 20,
            });
            p.player.camera.setCamera(CameraPreset.Free, {
                location: { x: 795, y: -40, z: 563 },
                facingLocation: { x: 780, y: -42, z: 576 },
            });
        });
    }
}
